package edu.corn.job.core.statemachine;

import edu.corn.job.core.engine.JobEngine;
import edu.corn.job.core.engine.JobOptionFactory;
import edu.corn.statemachine.core.DefaultSyncStateMachine;
import edu.corn.statemachine.core.StateMachine;


public class JobStatemachineFactory extends DefaultSyncStateMachine<JobStateEnum, JobEventEnum> {


    public static StateMachine<JobStateEnum, JobEventEnum> getStateMachine(JobEngine engine) {
        // 构建实例，添加操作。
        JobOptionFactory jobOptionFactory = new JobOptionFactory(engine);
        StateMachineBuilder<JobStateEnum, JobEventEnum> jobStateMachineBuilder = new StateMachineBuilder<>();


        jobStateMachineBuilder.with().sourceState(JobStateEnum.NEW)
                .targetState(JobStateEnum.RUNNABLE)
                .eventType(JobEventEnum.NEW)
                .action(jobOptionFactory.submitNew2WaitingAction())
                .done()
                .and()
                // 添加递交job的action
                .with().sourceState(JobStateEnum.RUNNABLE)
                .targetState(JobStateEnum.RUNNING)
                .eventType(JobEventEnum.SUBMIT)
                .action(jobOptionFactory.submitWaiting2RunningAction())
                .done()
                .and()
                // 添加被挂起的job的action
                .with().sourceState(JobStateEnum.RUNNING)
                .targetState(JobStateEnum.PENDING)
                .eventType(JobEventEnum.PENDING)
                .action(jobOptionFactory.submitWaiting2PendingAction())
                .done();


        return jobStateMachineBuilder.build();
    }

}
